function Unit() {
    this.pos = { x: 0, y: 0 };
    this.size = { x: 32, y: 32 };
	this.realSize = { x: 32, y: 32 };
	this.spriteSize = { x: 32, y: 32 };
    this.speed =  175;
	this.refOfSprite;
	this.die;
	this.life;
	this.maxLife;
	this.posInMap = { x: 0, y: 0 };	
	this.direction = {};
	this.radius;
	
	this.getPostionInMapX = function() {
		return this.posInMap.x;
	};
	
	this.getPostionInMapY = function() {
		return this.posInMap.y;
	};
	
	this.setLife = function(life) {
		this.life = life;
		this.maxLife = life;
	};
	
	this.setRadius = function(radius) {
		this.radius = radius;
	};
	
	this.getRadius = function() {
		return this.radius;
	};
	
	this.isAlive = function() {
		return !(this.die);
	};
	
	this.updateMovement = function(elapsed, keysDown) {
		// handle input to move the aniamls
        var move = { x: 0, y: 0 };
		elapsed *=0.25;
		var deplacement = this.speed * elapsed;
		
        if ((this.direction[Keys.Left]) && !(this.direction[Keys.Right]) && !(this.direction[Keys.Up]) && !(this.direction[Keys.Down])) {
            move.x -= deplacement;
            this.refOfSprite.move(MOVE.LEFT);
        }
        else if ((this.direction[Keys.Right]) && !(this.direction[Keys.Left]) && !(this.direction[Keys.Up]) && !(this.direction[Keys.Down])) { 
            move.x += deplacement;
            this.refOfSprite.move(MOVE.RIGHT);
        }
        else if ((this.direction[Keys.Up]) && !(this.direction[Keys.Right]) && !(this.direction[Keys.Left]) && !(this.direction[Keys.Down])) {
            move.y -= deplacement;
            this.refOfSprite.move(MOVE.UP);
        }
        else if ((this.direction[Keys.Down]) && !(this.direction[Keys.Right]) && !(this.direction[Keys.Up]) && !(this.direction[Keys.Left])) {
            move.y += deplacement;
            this.refOfSprite.move(MOVE.DOWN);
        }
        else if ((this.direction[Keys.Left]) && !(this.direction[Keys.Right]) && (this.direction[Keys.Up]) && !(this.direction[Keys.Down])) {
            move.x -= deplacement;
            move.y -= deplacement;
			this.refOfSprite.move(MOVE.LEFT);
        }
        else if ((this.direction[Keys.Right]) && !(this.direction[Keys.Left]) && (this.direction[Keys.Up]) && !(this.direction[Keys.Down])) { 
            move.x += deplacement;
            move.y -= deplacement;
			this.refOfSprite.move(MOVE.RIGHT);
        }
        else if ((this.direction[Keys.Left]) && !(this.direction[Keys.Right]) && !(this.direction[Keys.Up]) && (this.direction[Keys.Down])) {
            move.x -= deplacement;
            move.y += deplacement;
			this.refOfSprite.move(MOVE.LEFT);
        }
        else if ((this.direction[Keys.Right]) && !(this.direction[Keys.Left]) && !(this.direction[Keys.Up]) && (this.direction[Keys.Down])) { 
            move.x += deplacement;
            move.y += deplacement;
			this.refOfSprite.move(MOVE.RIGHT);
        }
		
		var wall = levels[currentLevel].moveEntityWall(this.pos, this.size, this.realSize, move);
		if(wall) {
			this.changeDirection();
		}
		else {
			this.pos = levels[currentLevel].moveEntity(this.pos, this.size, this.realSize, move);
			this.posInMap.x = Math.floor((this.pos.x + (this.size.x / 2)) / tileSize);
			this.posInMap.y = Math.floor((this.pos.y + (this.size.y / 2)) / tileSize);
		}
	}
	
	this.randomDirection = function() {
		// 3% chance to change direction
		if (Math.random() < 0.03) {
			this.changeDirection();
		}
	}
	this.changeDirection = function() {
		var r = Math.floor(Math.random() * 10);
		switch(r) {
			case 0 : 
				this.direction[Keys.Left] = true;
				this.direction[Keys.Right] = false;
				this.direction[Keys.Up] = false;
				this.direction[Keys.Down] = false;
				break;
			case 1 : 
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = true;
				this.direction[Keys.Up] = false;
				this.direction[Keys.Down] = false;
				break;
			case 2 : 
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = false;
				this.direction[Keys.Up] = true;
				this.direction[Keys.Down] = false;
				break;
			case 3 : 
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = false;
				this.direction[Keys.Up] = false;
				this.direction[Keys.Down] = true;
				break;
			case 4 : 
				this.direction[Keys.Left] = true;
				this.direction[Keys.Right] = false;
				this.direction[Keys.Up] = true;
				this.direction[Keys.Down] = false;
				break;
			case 5 : 
				this.direction[Keys.Left] = true;
				this.direction[Keys.Right] = false;
				this.direction[Keys.Up] = false;
				this.direction[Keys.Down] = true;
				break;
			case 6 : 
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = true;
				this.direction[Keys.Up] = true;
				this.direction[Keys.Down] = false;
				break;
			case 7 : 
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = true;
				this.direction[Keys.Up] = false;
				this.direction[Keys.Down] = true;
				break;
			default :
				this.direction[Keys.Left] = false;
				this.direction[Keys.Right] = true;
				this.direction[Keys.Up] = true;
				this.direction[Keys.Down] = false;
				break;
		}
	};
	
	this.draw = function(canvas, context, camera) {
		if(!this.isAlive()) {
			return;
		}
		// camera position in map
		var cx = Math.floor((player.pos.x + player.size.x / 2) / tileSize);
		var cy = Math.floor((player.pos.y + player.size.y / 2) / tileSize);
		// animals position in map
		var ax = Math.floor((this.pos.x + this.size.x / 2) / tileSize);
		var ay = Math.floor((this.pos.y + this.size.y / 2) / tileSize);
		
		if (levels[currentLevel].isTileVisible(cx, cy, ax, ay, player.getRadius())) {
			this.refOfSprite.drawSprite(context,
				Math.floor(this.pos.x - camera.x)-(tileSize-this.realSize.x)/2,
				Math.floor(this.pos.y - camera.y)-(tileSize-this.size.y)/2
				);
		}
	};
}